<template>
	<div class="poster">
		<list-title :data="header"></list-title>
		<el-form :model="expertsForm" :rules="rules" ref="expertsForm" label-width="200px">
			<el-form-item label="专家姓名" style="width: 600px;">
				<el-input v-model="expertsForm.data.name" placeholder="专家姓名"></el-input>
			</el-form-item>
			<el-form-item label="头像" prop="data.avatar" style="width: 565px;">
				<upload-oss 
				:stsdomain="this.$domain.ac" 
				:ossdomain="this.$ossdomain" 
				:limit="1" 
				:fileList="expertsForm.data.avatar" 
				:maxSize="20" 
				@insertFile="insertFile"
				@delFile="delFile">
				</upload-oss>
			</el-form-item>
			<el-form-item label="擅长" prop="data.goodat" style="width: 600px;">
				<el-input v-model="expertsForm.data.goodat" placeholder="擅长"></el-input>
			</el-form-item>
			<el-form-item label="鉴定费用" prop="data.authenticate_cost" style="width: 600px;">
				<el-input v-model="expertsForm.data.authenticate_cost" placeholder="鉴定费用"></el-input>
			</el-form-item>
			<el-form-item label="鉴定数量" prop="data.identified" style="width: 600px;">
				<el-input v-model="expertsForm.data.identified" placeholder="鉴定数量"></el-input>
			</el-form-item>
			<el-form-item label="是否显示" prop="data.display">
				<el-switch v-model="expertsForm.data.display"></el-switch>
			</el-form-item>
			<el-form-item label="简介" prop="data.introduce" style="width: 830px;">
				<el-input type="textarea" :rows="3" placeholder="请输入简介" v-model="expertsForm.data.introduce"></el-input>
			</el-form-item>
			<el-form-item>
				<el-button type="primary" @click="submitForm('expertsForm')" :disabled='expertsForm.submitLock'>
					确 定 <span class="el-icon-loading" v-if='expertsForm.submitLock'></span>
				</el-button>
				<el-button @click="resetForm('expertsForm')" v-if='expertsForm.data.id==0'>重 置</el-button>
			</el-form-item>
		</el-form>
	</div>
</template>

<script>
import ListTitle from "pc/common/Title"
import UploadOss from "pc/common/UploadOss"
import {changeToBool, changeUploadFormatBack} from "static/js/common.js"

export default {
	inject: ['reload'],
	data() {
		return {
			/*头部数据*/
			header: {
				title: '添加老师',
				reload: false,
				buttons: [
					{
						title: '老师列表',
						type: 'primary',
						pathName: 'pc/epi/pj8/ExpertsList'
					}
				]
			},
			categoryList: [],
			/*字段数据*/
			expertsForm: {
				submitLock: false,
				parentid: '',//选择的栏目ID
				data: {
					id: 0,
					name: '',
					avatar: [],
					goodat: '',
					authenticate_cost: 0,
					identified: 0,
					introduce: '',
					display: true,
					add_time: '',
				}
			},
			rules: {
				data: {
					name: [
						{ required: true, message: '请输入专家姓名', trigger: 'blur' },
						{ min: 2, max: 16, message: '长度在 2 到 16 个字符', trigger: 'blur' }
					],
					goodat: [
						{ required: true, message: '请输入擅长', trigger: 'blur' },
						{ min: 2, max: 32, message: '长度在 2 到 500 个字符', trigger: 'blur' }
					]
				}
			}
		};
	},
	components: {
		ListTitle,
		UploadOss
	},
	created: function() {
		/*如果是修改，取出旧数据*/
		if(this.$route.query.id){
			this.getExperts(this.$route.query.id)
		}
	},
	methods: {
		/*取出需要修改的数据*/
		getExperts: function(){
			this.header.title = '编辑老师'
			/*取出需要修改的数据*/
			this.expertsForm.submitLock = true;
			this.$get(this.$api.modifyExperts, {
				'id': this.$route.query.id
			}).then(res => {
				if(res.data.code == 0){
					// console.log(res.data.data)
					this.expertsForm.data.id = res.data.data.id
					this.expertsForm.data.name = res.data.data.name
					this.expertsForm.data.avatar = changeUploadFormatBack(res.data.data.avatar)
					this.expertsForm.data.goodat = res.data.data.goodat
					this.expertsForm.data.authenticate_cost = res.data.data.authenticate_cost
					this.expertsForm.data.identified = res.data.data.identified
					this.expertsForm.data.introduce = res.data.data.introduce
					this.expertsForm.data.display = changeToBool(res.data.data.display)
					this.expertsForm.data.add_time = res.data.data.add_time
				}else{
					this.$message({
						showClose: true,
						message: res.data.info,
						type: 'error'
					});
				}
				this.expertsForm.submitLock = false;
			})
			.catch(err => {})
			.finally(() => {}) 
		},
		/*提交添加、修改表单*/
		submitForm(formName) {
			if(!this.expertsForm.submitLock){
				this.$refs[formName].validate((valid) => {
					if (valid) {
						// console.log(this.expertsForm.data)
						// return
						this.expertsForm.submitLock = true;
						/*添加、修改分类*/
						let apiUrl = this.$api.addExperts//默认是添加
						if(this.expertsForm.data.id > 0){//修改
							apiUrl = this.$api.modifyExperts
						}
						/*添加菜单*/
						let data = JSON.parse(JSON.stringify(this.expertsForm.data))

						this.$post(apiUrl, {
							Experts: data,
						}).then(res => {
							// console.log(res);
							/*成功*/
							if(res.data.code == 0){
								this.$message({
									showClose: true,
									message: res.data.info,
									type: 'success'
								});
								if(this.expertsForm.data.id > 0){
									this.$router.go(-1)	
								}else{
									this.$refs[formName].resetFields();
								}
							}else{
								this.$message({
									showClose: true,
									message: res.data.info,
									type: 'error'
								});
							}
							this.expertsForm.submitLock = false;
						});
					} else {
						console.log('error submit!!');
						return false;
					}
				});
			}
		},
		/*重置表单*/
		resetForm(formName) {
			this.$refs[formName].resetFields();
		},
		/*插入上传的文件*/
		insertFile(file){
			// console.log(file instanceof Array)
			if(file instanceof Array){//如果是数组的话，说明是图片
				for(let i=0; i<file.length; i++){
					this.expertsForm.data.avatar.push(
						{
							name: '',
							url:  file[i].url
						}
					)
				}
			}else{//单个文件路径

			}
		},
		/*删除上传的文件*/
		delFile(index){
			if(index >= 0){//图片
				this.expertsForm.data.avatar.splice(index, 1)
			}else{//文件路径

			}
		},
	},
    computed: {
    	
    }
};
</script>

<style lang="stylus" scoped>
.poster
	form
		margin-top: 10px
</style>